<?php
/**
 * Description of order
 *
 * @author Henson
 */
class Order extends Base{
	
	private $orderTable = "tbl_order";
	
	private $eventTable = "tbl_event";
	
	function updateOrder($updateArr, $oid, $eid=0)
	{
		$updateArr['update_time'] = time();
		$where = " where id=".$oid.(intval($eid) > 0 ? " and eid={$eid}" : "");
		return $this->db->update($updateArr, $this->orderTable, $where);
	}
	
	public function insertOrder($insertArr)
	{
		//$insertArr['aid'] = $aid;
		$insertArr['insert_time'] = time();
		$insertArr['del'] = '0';
		return $this->db->insert($insertArr, $this->orderTable);
	}
	
	public function delOrder($eid)
	{
		$updateArr = array(
			'del'			=> '1',
			'update_time'	=> time()
		);
		$where = " where id=".$eid;
		return $this->db->update($updateArr, $this->orderTable, $where);
	}
	
	public function getOrderList($p)
	{
		$limit = "limit ".($p -1 ) * $this->pageRow .",".$this->pageRow;
		$sql = "select o.*,e.title from {$this->orderTable} o  
			left join {$this->eventTable} e on o.eid=e.id
			where o.del='0' order by o.insert_time desc,id {$limit}";
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}
	
	public function dealOrder($orderArr)
	{
		$orderArr['order_num'] = str_pad($orderArr['id'], 6, '0', STR_PAD_LEFT);
		$orderArr['status'] = $orderArr['cancel']=='1' ? 'Cancel' : 'Normal';
		return $orderArr;
	}
			
	public function getOrderTotal()
	{
		$where = "where del='0'";
		return $this->getTotalNum($this->orderTable, $where);
	}
	
	public function getCurrentOrderByUid($uid)
	{
		$now = time();
		$sql = "select o.*,
				e.id as eid,e.title,e.address,e.description,e.start_time,e.end_time
				from {$this->orderTable} o  
				left join {$this->eventTable} e on o.eid=e.id 
				where o.del='0' and o.cancel='0' and e.start_time > {$now} and uid=".$uid;
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}
	
	public function getOldOrderTotal($where)
	{
		$sql = "select count(o.id) from {$this->orderTable} o  
				left join {$this->eventTable} e on o.eid=e.id 
				{$where}";
		return $this->db->loadResult($sql);
	}
	
	public function getOldOrderList($where, $orderBy, $limit)
	{
		$sql = "select o.*,e.id as eid,e.title from {$this->orderTable} o  
			left join {$this->eventTable} e on o.eid=e.id
			{$where} {$orderBy} {$limit}";
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}
	
	public function getOrderByOid($oid)
	{
		$sql = "select o.*,e.id as eid,e.title,e.start_time,e.limit_time from {$this->orderTable} o  
			left join {$this->eventTable} e on o.eid=e.id where o.del='0' and o.id=".$oid;
		return $this->db->loadRow($sql);
	}
	
	public function getOrderedUserInfoByEid($eid)
	{
		$sql = "select u.first_name, u.email from {$this->orderTable} o left join tbl_user u on u.id=o.uid
			where o.del='0' and o.cancel='0' and o.eid=".$eid;
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}
}

?>
